//
//  ChangePasswordViewController.m
//  TravelFollowYourHeart
//
//  Created by ENERGY on 2017/12/20.
//  Copyright © 2017年 Lwx. All rights reserved.
//

#import "ChangePasswordViewController.h"
#import "ChangePasswordCellTableViewCell.h"
#define verificationCodeBtnW 80
#define border 10
static int i = 29;
@interface ChangePasswordViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UIView * headerView;
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIView * footerView;
@property (nonatomic, strong)UIButton * verificationCodeBtn;
@property (nonatomic, strong)UIImageView * portraitImage;
@property (nonatomic, strong)UITextField * phoneTextField;
@property (nonatomic, strong)UITextField * verificationCodeTextField;
@property (nonatomic, strong)UITextField * passwordTextField;
@property (nonatomic, strong)UITextField * passwordAgainTextField;
//@property(nonatomic,strong) UIButton *getNmuber;// 定时器
@property(nonatomic,strong) NSTimer *timmer;
- (void)test:(NSTimer *)timmer;
@end

@implementation ChangePasswordViewController(networking)
//获取验证码
- (void)getNumberWithMessage{
    NSDictionary * dic = @{
                           @"mobile":self.phoneTextField.text
                           };
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:Url(@"/login/getVertify") andParas:dic andTransferGottenData:^(id obj, NSError *error) {
        if (error) {
            [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
        }else{
            if ([obj[@"code"] integerValue] == 100000){
                
                self.verificationCodeBtn.enabled = NO;
                [self.verificationCodeBtn setBackgroundColor:[UIColor grayColor]];
                //初始化定时器
                self.timmer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(test:) userInfo:nil repeats:YES];
            }else{
                [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
            }
        }
    }];
}

//修改密码
- (void)forgetPass{
    NSDictionary * dic = @{
                           @"mobile":self.phoneTextField.text,
                           @"smsCode":self.verificationCodeTextField.text,
                           @"pass":self.passwordTextField.text,
                           @"surePass":self.passwordAgainTextField.text
                           };
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:Url(@"/login/forgetPass") andParas:dic andTransferGottenData:^(id obj, NSError *error) {
        if (error) {
            [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
        }else{
            if ([obj[@"code"] integerValue] == 100000){
                [self.navigationController popViewControllerAnimated:YES];
                [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
            }else{
                [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
            }
        }
    }];
}


@end

@implementation ChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
    [self.view addSubview:self.tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
        _headerView.backgroundColor = getColor(bgColor);
    }return _headerView;
}

- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT - 64 - 8 - 44 * 5 / WIDTH_5S_SCALE)];
        _footerView.backgroundColor = getColor(bgColor);
        //确定按钮
        UIButton *saveButton = [[UIButton alloc] init];
        saveButton.titleLabel.font = DEF_FontSize_14;
        [saveButton setTitle:@"确定" forState:UIControlStateNormal];
        [saveButton setTitleColor:getColor(whiteColor) forState:UIControlStateNormal];
        [saveButton setBackgroundColor:getColor(mainColor)];
        saveButton.layer.cornerRadius = 18 / WIDTH_5S_SCALE;
        saveButton.layer.masksToBounds = YES;
        saveButton.userInteractionEnabled = YES;
        [saveButton addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [_footerView addSubview:saveButton];
        [saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_footerView).offset(10 / WIDTH_5S_SCALE);
            make.right.equalTo(_footerView).offset(-10 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(36 / WIDTH_5S_SCALE);
            make.bottom.equalTo(_footerView).offset(-100 / WIDTH_5S_SCALE);
        }];
    }
    return _footerView;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
        _tableView.separatorInset = UIEdgeInsetsZero;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = self.headerView;
        _tableView.tableFooterView = self.footerView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = getColor(bgColor);
    }return _tableView;
}

- (void)sureBtnClick{
    NSLog(@"确定修改密码");
    [self forgetPass];
}

#pragma tableViewDelegate和tableViewDatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell * cell = nil;
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    int portraitW = cell.height - 2 * border;
    int contentTextFieldX = portraitW + 2 * border;
    
    //线
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = getColor(dividerColor);
    [cell.contentView addSubview:lineView];
    if (indexPath.row != 0) {
        [cell.contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell.contentView).offset(10 / WIDTH_5S_SCALE);
            make.right.equalTo(cell.contentView).offset(-10 / WIDTH_5S_SCALE);
            make.top.equalTo(cell.contentView);
            make.height.mas_equalTo(0.5);
        }];
    }
    if (indexPath.row != 1) {
        _portraitImage = [[UIImageView alloc] init];
        _portraitImage.frame = CGRectMake(border, border, portraitW, portraitW);
        [cell.contentView addSubview:_portraitImage];
    }else{
        _verificationCodeBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - verificationCodeBtnW - border, border, verificationCodeBtnW, cell.height - 2* border)];
        _verificationCodeBtn.layer.cornerRadius = 8;
        _verificationCodeBtn.layer.masksToBounds = YES;
        [_verificationCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        _verificationCodeBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [_verificationCodeBtn setTitleColor:getColor(whiteColor) forState:UIControlStateNormal];
        [_verificationCodeBtn setBackgroundColor:getColor(mainColor)];
        [cell.contentView addSubview:_verificationCodeBtn];
        [_verificationCodeBtn addTarget:self action:@selector(sendNumber) forControlEvents:UIControlEventTouchUpInside];
    }
    int textFieldW = 150;
    if (indexPath.row == 0) {
        _portraitImage.image = [UIImage imageNamed:@"login_icon_accaunt"];
        _phoneTextField = [[UITextField alloc] init];
        _phoneTextField.font = [UIFont systemFontOfSize:14.0];
        [cell.contentView addSubview:_phoneTextField];
        _phoneTextField.frame = CGRectMake(contentTextFieldX, 0, textFieldW, cell.height);
        _phoneTextField.placeholder = @"请输入手机号";
    }
    if (indexPath.row == 1) {
        _verificationCodeTextField = [[UITextField alloc] init];
        _verificationCodeTextField.font = [UIFont systemFontOfSize:14.0];
        [cell.contentView addSubview:_verificationCodeTextField];
        _verificationCodeTextField.frame = CGRectMake(contentTextFieldX, 0, textFieldW, cell.height);
//        _verificationCodeTextField.backgroundColor = [UIColor orangeColor];
        _verificationCodeTextField.placeholder = @"请输入验证码";
    }
    if (indexPath.row == 2) {
        _portraitImage.image = [UIImage imageNamed:@"longin_password"];
        _passwordTextField = [[UITextField alloc] init];
        _passwordTextField.font = [UIFont systemFontOfSize:14.0];
        [cell.contentView addSubview:_passwordTextField];
        _passwordTextField.frame = CGRectMake(contentTextFieldX, 0, textFieldW, cell.height);
//        _passwordTextField.backgroundColor = [UIColor orangeColor];
        _passwordTextField.placeholder = @"请输入密码";
    }
    if (indexPath.row == 3) {
        _portraitImage.image = [UIImage imageNamed:@"longin_password"];
        _passwordAgainTextField = [[UITextField alloc] init];
        _passwordAgainTextField.font = [UIFont systemFontOfSize:14.0];
        [cell.contentView addSubview:_passwordAgainTextField];
        _passwordAgainTextField.frame = CGRectMake(contentTextFieldX, 0, textFieldW, cell.height);
//        _passwordAgainTextField.backgroundColor = [UIColor orangeColor];
        _passwordAgainTextField.placeholder = @"请输再次输入密码";
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44 / WIDTH_5S_SCALE;
}

- (void)sendNumber{
    NSLog(@"发送验证码");
    if (self.phoneTextField.text.length == 0) {
        [[HUDHelper sharedInstance]tipMessage:@"请输入手机号"];
        return;
    }
    [self.view endEditing:YES];
    [self getNumberWithMessage];//获取验证码

}

- (void)test:(NSTimer *)timmer{
    NSString *str = [NSString stringWithFormat:@"%d后重试",i];
    [self.verificationCodeBtn setTitle:str forState:UIControlStateDisabled];
    i--;
    if (i == -1) {
        [timmer invalidate];
        self.verificationCodeBtn.enabled = YES;
        [self.verificationCodeBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
        [self.verificationCodeBtn setBackgroundColor:getColor(mainColor)];
        i = 29;
    }
}

@end
